package com.jinke.api.modules.app.config;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jinke.api.common.exception.Asserts;
import com.jinke.api.modules.app.controller.res.RuleSurchargeImportDto;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

/**
 * @projectName:
 * @title: PriceRuleSheetImportListener
 * @description:
 * @see:
 * @author: hhp
 * @version: 1.0.0
 * @createTime: 2024/7/31 16:02
 */
@Slf4j
@Component
@Scope("prototype")
public class PriceRuleSheetImportListener extends AnalysisEventListener<RuleSurchargeImportDto.PriceRuleItem> {

    @Getter
    private List<RuleSurchargeImportDto.PriceRuleItem> priceRuleItems = Lists.newArrayList();
    private int curIndex = 0;

    @Override
    public void invoke(RuleSurchargeImportDto.PriceRuleItem priceRuleItem, AnalysisContext analysisContext) {
        if (ObjectUtil.isNotNull(priceRuleItem.getZone1())) {
            priceRuleItem.setZone1(priceRuleItem.getZone1().setScale(2, RoundingMode.UP));
        }
        if (ObjectUtil.isNotNull(priceRuleItem.getZone2())) {
            priceRuleItem.setZone2(priceRuleItem.getZone2().setScale(2, RoundingMode.UP));
        }
        if (ObjectUtil.isNotNull(priceRuleItem.getZone3())) {
            priceRuleItem.setZone3(priceRuleItem.getZone3().setScale(2, RoundingMode.UP));
        }
        if (ObjectUtil.isNotNull(priceRuleItem.getZone4())) {
            priceRuleItem.setZone4(priceRuleItem.getZone4().setScale(2, RoundingMode.UP));
        }
        if (ObjectUtil.isNotNull(priceRuleItem.getZone5())) {
            priceRuleItem.setZone5(priceRuleItem.getZone5().setScale(2, RoundingMode.UP));
        }
        if (ObjectUtil.isNotNull(priceRuleItem.getZone6())) {
            priceRuleItem.setZone6(priceRuleItem.getZone6().setScale(2, RoundingMode.UP));
        }
        if (ObjectUtil.isNotNull(priceRuleItem.getZone7())) {
            priceRuleItem.setZone7(priceRuleItem.getZone7().setScale(2, RoundingMode.UP));
        }
        priceRuleItems.add(priceRuleItem);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 全部数据读取完后调用

    }

    public void setPriceRuleItems(List<RuleSurchargeImportDto.PriceRuleItem> priceRuleItems) {
        this.priceRuleItems = priceRuleItems;
    }

    public void setCurIndex(int curIndex) {
        this.curIndex = curIndex;
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        Map<Integer, String> integerStringMap = getPriceTemplateHeadMaps().get(curIndex);
        log.info("第一个sheet:标准表头:{}, 当前第{}的表头:{}", integerStringMap, curIndex, headMap);
        if (!areMapsEqualIgnoreCase(integerStringMap, headMap)) {
            Asserts.fail("第一个sheet文件内容格式错误, 请上传标准价格表excel");
        }
        curIndex++;
    }

    private List<Map<Integer, String>> getPriceTemplateHeadMaps() {
        List<Map<Integer, String>> headMaps = Lists.newArrayList();
        Map<Integer, String> head1 = MapUtil.newHashMap();
        head1.put(0, "weight no to");
        head1.put(1, "Fedex");
        head1.put(2, null);
        head1.put(3, null);
        head1.put(4, null);
        head1.put(5, null);
        head1.put(6, null);
        head1.put(7, null);
        headMaps.add(head1);
        Map<Integer, String> head2 = MapUtil.newHashMap();
        head2.put(0, "Lbs");
        head2.put(1, "zone1");
        head2.put(2, "zone2");
        head2.put(3, "zone3");
        head2.put(4, "zone4");
        head2.put(5, "zone5");
        head2.put(6, "zone6");
        head2.put(7, "zone7");
        headMaps.add(head2);
        return headMaps;
    }

    private static boolean areMapsEqualIgnoreCase(Map<Integer, String> map1, Map<Integer, String> map2) {
        if (map1.size()!= map2.size()) {
            return false;
        }

        for (Map.Entry<Integer, String> entry : map1.entrySet()) {
            Integer key = entry.getKey();
            String value1 = entry.getValue();
            String value2 = map2.get(key);

            if (!(StrUtil.isEmpty(value1) && StrUtil.isEmpty(value2))) {
                if (StrUtil.isEmpty(value2) ||!value1.equalsIgnoreCase(value2)) {
                    return false;
                }
            }
        }
        return true;
    }
}
